export default {
	namespaced: true,
	state: {
		shoppingCartList: uni.getStorageSync('Flower')['shoppingCartList'] || [],
	},
	getters: {
		selectNum(state) {
			let arr = state.selectNum = state.shoppingCartList.filter(ele => ele.isChecked == true) || []
			return arr.length
		},
		allNum(state) {
			return state.shoppingCartList.reduce((sum, ele) => {
				sum += ele.num
				return sum
			}, 0)
		},
		totalNum(state) {
			return state.shoppingCartList.reduce((sum, ele) => {
				if (ele.isChecked) {
					sum += ele.num
				}
				return sum
			}, 0)
		},
		totalPrice(state) {
			return state.shoppingCartList.reduce((sum, ele) => {
				if (ele.isChecked) {
					sum += ele.s_good.sale_price * ele.num
				}
				return sum
			}, 0)
		}
	},
	mutations: {
		setshoppingCart(state, shoppingCartList) {
			shoppingCartList.forEach(ele => ele.isChecked = false)
			state.shoppingCartList = shoppingCartList
			let Flower = uni.getStorageSync('Flower')
			if (Flower) {
				Flower.shoppingCartList = shoppingCartList
				uni.setStorageSync('Flower', Flower)
			}
		},
		// 全选
		allChekced(state, payload) {
			state.shoppingCartList.forEach(ele => {
				ele.isChecked = payload
			});
		},
		// 单选
		oneChecked(state, id) {
			let prod = state.shoppingCartList.find(ele => ele.id == id)
			// 判断存不存在
			prod && (prod.isChecked = !prod.isChecked)
		},
	}
}
